import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

class PointsSurface extends JPanel implements ActionListener{
    private final int DELAY=1000;
    private Timer timer;

    public PointsSurface(){
        initTimer();
    }

    private void initTimer(){
        timer=new Timer(DELAY,this);
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }

    private void doDrawing(Graphics g){
        Graphics2D g2d= (Graphics2D) g;
        g2d.setPaint(Color.blue);

        int w=getWidth();
        int h=getHeight();

        Random r=new Random();

        for (int i=0;i<2000;++i){
            int x=r.nextInt()%w;
            int y=r.nextInt()%h;
            g2d.drawLine(x, y, x, y);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    Timer getTimer(){
        return timer;
    }
}

public class PointsEx extends JFrame{

    public PointsEx(){
        initUI();
    }

    private void initUI(){
        final PointsSurface surface=new PointsSurface();
        add(surface);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Timer timer=surface.getTimer();
                timer.stop();
            }
        });

        setTitle("Points");
        setSize(350,250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                PointsEx ex=new PointsEx();
                ex.setVisible(true);
            }
        });
    }
}
